//
//  MujiDictionaryView.m
//  testdemo
//
//  Created by yule on 2017/5/23.
//  Copyright © 2017年 yule. All rights reserved.
//

#import "MujiDubDictionaryView.h"
#import "MujiDubHeader.h"
#import "MujiDubNetworkAPI.h"
#import <AVFoundation/AVFoundation.h>

@interface MujiDubDictionaryView ()

@property (nonatomic, strong) UIView   *containView;
@property (nonatomic, strong) UIWebView   *webView;
@property (nonatomic, strong) UIButton   *closeBtn;


@end

@implementation MujiDubDictionaryView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        
        [self createUI];
    }
    
    return self;
}

-(void)createUI
{
    self.backgroundColor = ColorWithRGBA(0, 0, 0, 0);
    self.userInteractionEnabled = YES;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 265)];
    [self addSubview:view];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTap:)];
    [view addGestureRecognizer:tap];

    
    self.containView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 265, kScreenWidth, 265)];
    self.containView.backgroundColor = [UIColor blackColor];
    self.containView.userInteractionEnabled = YES;
    
    [self addSubview:self.containView];
    
//    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//    BOOL ok;
//    NSError *setCategoryError = nil;
//    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback        error:&setCategoryError];
//    if (!ok) {
//        NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
//    }
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 265)];
    [self.containView addSubview:self.webView];
    self.webView.backgroundColor = ColorWithRGBA(68, 68, 68, 1.0);
    [self.webView setMediaPlaybackRequiresUserAction:NO];
    self.webView.scrollView.backgroundColor = ColorWithRGBA(68, 68, 68, 1.0);
    
    UIImageView *triangle = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 14)/2, -7, 14, 8)];
    triangle.image = [UIImage imageNamed:@"icon_dub_dicttriangle"];
    [self.containView addSubview:triangle];

    self.closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 29 - 15, 23, 29, 29)];
    [self.closeBtn setImage:[UIImage imageNamed:@"icon_dub_dictclose"] forState:UIControlStateNormal];
    [self.closeBtn setImage:[UIImage imageNamed:@"icon_dub_dictclose"] forState:UIControlStateSelected];
    [self.closeBtn addTarget:self action:@selector(closeBtnActin:) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.closeBtn];
    
}

- (void)refreshDataWithUrl:(NSString *)string
{
    
    if (self.webView && string.length != 0) {
        
        NSString *url = [NSString stringWithFormat:@"%@?%@", kDict_Url, string];
        
        NSLog(@"  dictUrl  %@", url);
        
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:url]];
        [self.webView loadRequest:request];

    }
}
-(void)closeTap:(UITapGestureRecognizer *)tapGesture
{
    if (self.dictViewAction) {
        self.dictViewAction(0);
    }

    
}

- (void)closeBtnActin:(UIButton *)btn
{
    if (self.dictViewAction) {
        self.dictViewAction(0);
    }
}


@end
